package classes
{
	import flash.display.Graphics;
	import flash.events.MouseEvent;
	
	import mx.core.UIComponent;
	
	public class DrawFoundations
	{
		//set up all required variables
		private var _DrawTarget:UIComponent;
		private var arrLines:Array = new Array();
		private var _DrawCurrentLine:Array;
		
		
		
		public function DrawFoundations(target:UIComponent)
		{
			_DrawTarget = target;
			_DrawTarget.addEventListener(MouseEvent.MOUSE_DOWN, DrawMouseDown);
			_DrawTarget.addEventListener(MouseEvent.MOUSE_UP, DrawMouseUp);
			_DrawTarget.addEventListener(MouseEvent.MOUSE_OUT, DrawMouseUp);
		}
		
		
		public function DrawNewLine():void
        {
            _DrawCurrentLine = new Array();
            arrLines.push(_DrawCurrentLine);
            
            if(_DrawCurrentLine.length > 10 )
               _DrawCurrentLine.shift();
            
        }
        
        public function addPoint(xIN:int, yIN:int):void
        {
            _DrawCurrentLine.push({x:xIN, y:yIN});
            
            _DrawTarget.invalidateDisplayList();
        }
        
        
        public function drawLines( graphics:Graphics, lw:Number, lc:uint, la:Number ):void
        {
            graphics.clear();
            graphics.lineStyle(lw,lc,la);
            
            for( var j:int = 0; j<arrLines.length; j++ )
            {
                var line:Array = arrLines[j];
                
                for( var i:int = 0; i<line.length; i++ )
                {
                    var p:Object = line[i];
                    if( i == 0 )
                    {
                        graphics.moveTo( p.x, p.y );
                    }    
                    else
                    {
                        graphics.lineTo( p.x, p.y );
                    }    
                }
            }
        }
        
        private function moveEvent(event:MouseEvent):void
        {
            addPoint( event.localX, event.localY );                
        }
        
        private function DrawMouseDown(event:MouseEvent):void
        {
            _DrawTarget.addEventListener(MouseEvent.MOUSE_MOVE, moveEvent);

            DrawNewLine();
        }
        
        private function DrawMouseUp(event:MouseEvent):void
        {
            _DrawTarget.removeEventListener(MouseEvent.MOUSE_MOVE, moveEvent);
        }

	}
}